package top.korori.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.korori.common.model.PageData;
import top.korori.common.model.PageParam;
import top.korori.common.model.Result;
import top.korori.system.annotation.OptLog;
import top.korori.system.annotation.PreAuth;
import top.korori.system.constant.SysOptTypeConst;
import top.korori.system.entity.SysUser;
import top.korori.system.entity.bo.SysUserBo;
import top.korori.system.service.SysUserService;
import top.korori.system.entity.vo.SysUserVo;

import javax.validation.Valid;
import java.util.List;

/**
 * 
 * 后台用户 前端控制器
 * 
 *
 * @author korori
 */
@Api(tags = "用户信息")
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value = "获取用户列表",notes = "获取用户列表")
    @PreAuth("sys:user:list")
    @GetMapping("/list")
    public Result<PageData<SysUser>> list(SysUser sysUser, PageParam pageParam){
        PageData<SysUser> pageData = sysUserService.list(sysUser, pageParam);
        return Result.ok(pageData);
    }

    @ApiOperation("获取用户")
    @GetMapping("/get/{userId}")
    @PreAuth("sys:user:get")
    public Result<SysUserVo> get(@PathVariable Long userId){
        SysUserVo sysUserVo = sysUserService.get(userId);
        return Result.ok(sysUserVo);
    }

    @ApiOperation("添加用户")
    @OptLog(optType = SysOptTypeConst.ADD)
    @PreAuth("sys:user:add")
    @PostMapping("/add")
    public Result<Void> add(@RequestBody @Valid SysUserBo sysUserBo){
        sysUserService.add(sysUserBo);
        return Result.ok();
    }

    @ApiOperation("修改用户")
    @OptLog(optType = SysOptTypeConst.EDIT)
    @PreAuth("sys:user:edit")
    @PutMapping("/edit")
    public Result<Void> edit(@RequestBody SysUserBo sysUserBo){
        sysUserService.edit(sysUserBo);
        return Result.ok();
    }

    @ApiOperation("删除用户")
    @OptLog(optType = SysOptTypeConst.DEL)
    @PreAuth("sys:user:del")
    @DeleteMapping("/del/{userIds}")
    public Result<Void> del(@PathVariable List<Long> userIds){
        sysUserService.removeByIds(userIds);
        return Result.ok();
    }


}




























